package com.bj58.data.emailreports.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternUtil {

	
	
	/**匹配是否包含中文
	 * @param content
	 * @return
	 */
	public static boolean hasContainsChinese(String content){
		Pattern pattern=Pattern.compile("[\\u4e00-\\u9fa5]");
		Matcher matcher=pattern.matcher(content);
		return matcher.find();
	}
	
	
	/**是否包含英文
	 * @param content
	 * @return
	 */
	public static boolean hasContainsEn(String content){
		Pattern pattern=Pattern.compile("[\\[A-Za-z\\]]");
		Matcher matcher=pattern.matcher(content);
		return matcher.find();
	}
	
	
	/**是否是日期 (yyyy-MM-dd)
	 * @param content
	 * @return
	 */
	public static boolean isDate(String content){
		
		Pattern pattern=Pattern.compile("[0-9]{4}-[0-9]{2}-[0-9]{2}");
		Matcher matcher=pattern.matcher(content);
		return matcher.find();
	}
	
	
	/**是否只含有数字和小数点 (仅一个小数点)
	 * @param content
	 * @return
	 */
	public static boolean onlyHasNum (String content){
		Pattern pattern=Pattern.compile("^([0-9]{1,}\\.[0-9]{1,}|[0-9]{1,})$");
		Matcher matcher=pattern.matcher(content);
		return matcher.find();
	}
	
	
	/**邮件同环比阀值校验
	 * @param checkList
	 * @param threshold
	 * @return
	 */
	public static List<String> mailWarnCheck(List<String> checkList , double threshold){
		
		List<String> checkedDatas = new ArrayList<String>();
		
		for (String rowData : checkList) {
			StringBuffer newData = new StringBuffer();
			
			String[] rowSplit = rowData.split(";");
			for (String data : rowSplit) {
				if(data.contains("%") && data.contains("color")){
					double checkNum = Double.parseDouble(data.substring(data.indexOf(">") + 1 , data.indexOf("%")));
					
					if(Math.abs(checkNum) > threshold ){
						String color = "#FF87DD";
						if(checkNum < 0){
							color = "#77FFEE";
						}
						data = "<span style='background: "+color+"'>" +  checkNum + "%</span>";
					}
				}
				
				newData.append(data+";");
			}
			
			checkedDatas.add(newData.toString().substring(0,newData.toString().lastIndexOf(";")));
			
		}
		
		return checkedDatas;
	}
}
